package com.vic.common.util;

import java.io.*;
import java.util.List;
import java.util.UUID;

public class CommonUtil {
    private CommonUtil() {
    }

    public static String generateId() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 用Writer向文件中写入字符串
     *
     * @param id
     * @param content
     * @param path
     * @throws IOException
     */
    public static void writeStringByPrintWriter(String id, String content, String path) {
        File fileDir = new File(path);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File file = new File(path + File.separator + id);
        try (PrintWriter writer = new PrintWriter(file)) {
            writer.print(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("写入文件异常", e);
        }
    }

    /**
     * 根据文件名获取指定路径下文件
     *
     * @param dir
     * @param id
     * @return
     */
    public static File getFileByFileName(String dir, String id) {
        File fileDir = new File(dir);
        if (!fileDir.exists()) {
            return null;
        }
        File[] files = fileDir.listFiles();
        if (null == files) {
            return null;
        }
        for (File file : files) {
            if (file.getName().equals(id)) {
                return file;
            }
        }
        return null;
    }

    /**
     * @param file 输入流
     * @return java.lang.String
     * @desc 从流中获取字符串
     * @method getString
     * @author YulinChen
     * @date 2020/2/13 19:49
     */
    public static String getString(File file) {
        StringBuilder sb = null;
        try {
            FileInputStream in = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            sb = new StringBuilder();
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        File file = getFileByFileName("data/article", "a0de86426b484e03b27dbca74764e522");
        System.out.println(getString(file));
    }

    public static File[] getDirFiles(String path) {
        File fileDir = new File(path);
        if (!fileDir.exists()) {
            return null;
        }
        return fileDir.listFiles();
    }

    public static void ioCopy(InputStream inputStream, String pathOut) {
        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(pathOut));
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static long intOf(Object i) {
        try {
            return Integer.parseInt(String.valueOf(i));
        } catch (Exception e) {
            return 0;
        }
    }
}
